<?php
namespace App\Libs\Order;
use \Phalcon\DI;
use \App\Libs\Cache\Redis;
use \App\Libs\DB\PailePdo;
use App\Models\Goods\Goods;
/**
 * 获取订单列表
 */
class OrderList extends \App\Libs\Base\AccessObject
{

	/**
	*	获取订单列表
	*/
    public static function OrderList( $user , $params ){
		$where = " where uid = {$user->id} ";
		$order = " order by orders.ctime desc";
		$params = array_merge( ['size'=>10,'page'=>1] , $params );
		//订单状态
		if( is_numeric($params['status'])  ){
			if( $params['status'] == '99' ){
				//全部
			}else{
				switch( $params['status'] ){
					case 6:
						$where .= " and ( orders.status = 6 or  orders.status = 7)";
						break;
					case 5:
						$where .= " and ( orders.status = 5 or  orders.status = 8)";
						break;
					case 0:
						$where .= " and orders.stime > '".date("Y-m-d H:i:s")."'";
					default:
						$where .= " and orders.status = {$params['status']} ";
						break;
				}
			}
		}
		$limit = ' limit '.( ($params['page'] - 1)*$params['size'] ).','.$params['size'];
		
		$sql = "SELECT goods.id,goods.goods_name,goods.goods_price,goods.sale_price,goods.goods_img,orders.sum,orders.order_sn,orders.status,store.store_name,orders.ctime,orders.stime FROM t_paile_order orders LEFT JOIN t_paile_goods goods ON orders.gid = goods.id LEFT JOIN t_paile_store store ON store.id = goods.sid ".$where.$order.$limit;
		$result = PailePdo::findAll( $sql );
        foreach($result as $key => $val){
            $priceArray['goods_price'] = Goods::formatPrice($val['goods_price']);
            $priceArray['sale_price'] = Goods::formatPrice($val['sale_price']);
            $result[$key] = array_merge($val,$priceArray);
        }
		return $result; 
    }
	
	/**
	*	获取订单各总状态的数量
	*/
	public static function orderStatusCount( $user ){
		$sql = 'select count(*) count,status from t_paile_order where uid = '.$user->id.' GROUP BY `status`';
		$result = PailePdo::findAll( $sql );
		return $result;
	}
}


